<template>
  <div class="login-container">
    <el-form class="login-form">
      <el-form-item>
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                 @click.native.prevent="handleLogin">Login
      </el-button>
    </el-form>
  </div>
</template>
<script>
import { UserApi } from '@/api/user'

export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  methods: {
    async handleLogin() {
      const res = await UserApi.login(this.loginForm)
      localStorage.setItem('token', '123456')
      this.$router.push({ path: this.redirect || '/' })
    },
    // 获取路由参数
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  },
  mounted() {
    console.log(this.$store.state.user.role)
  }
}
</script>

<style lang="less">
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
  
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  
  .el-input input {
    background: transparent;
    border: none;
  }
}

.login-form {
  position: relative;
  width: 520px;
  max-width: 100%;
  padding: 160px 35px 0;
  margin: 0 auto;
  overflow: hidden;
}
</style>
